package cn.myspringboot.comment.controller;

import cn.myspringboot.anno.PreAuthorize;
import cn.myspringboot.comment.service.CommentService;
import cn.myspringboot.user.domain.Comment;
import cn.myspringboot.user.query.CommentQuery;
import cn.myspringboot.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@Api("评论相关接口")
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private MongoTemplate mongoTemplate;

    @PreAuthorize(name = "查询评论信息",sn = "comment:add",descs ="查询评论信息")
    @ApiOperation("查询评论信息")
    @GetMapping("/getComment/{articleId}")
    public List<Comment> getComment(@PathVariable("articleId") Long articleId) {
        return commentService.findCommentByBlogId(articleId);
    }

    @PreAuthorize(name = "添加或者修改评论信息",sn = "comment:add",descs ="添加或者修改评论信息")
    @ApiOperation("添加或者修改评论信息")
    @PostMapping
    public R add(@RequestBody Comment comment) {
        if(comment.getId() == null){
            comment.setCreateTime(new Date());
            comment.setState(1);
            commentService.saveComment(comment);
        }else {
            Query id = new Query(Criteria.where("id").is(comment.getId()));
            Comment one = this.mongoTemplate.findOne(id, Comment.class);
//            comment.setCreateTime(new Date());
            comment.setId(one.getId());
            this.mongoTemplate.save(comment);
        }
        return R.ok();
    }

    @PreAuthorize(name = "删除评论信息",sn = "comment:delete",descs ="删除评论信息")
    @ApiOperation("删除评论信息")
    @DeleteMapping("/{articleId}")
    public R delete(@PathVariable("articleId") String id) {
        Query getId =  new Query(Criteria.where("id").is(id));
        this.mongoTemplate.remove(getId, Comment.class);
        return R.ok();
    }

    /**
     * MongoDB 提供的PageRequest和Pageable来实现分页
     * @param commentQuery
     * @return
     */
    @PostMapping("/page")
    public R page(@RequestBody CommentQuery commentQuery){
        // 提供分页请求   PageRequest.of方法的第一个参数是从 0 开始的页码
        Pageable pageable = PageRequest.of(commentQuery.getPage() - 1, commentQuery.getSize());
        // 查询条件
        Query query = new Query(Criteria.where("state").is(1));
        if (commentQuery.getArticleId() != null) {
            query.addCriteria(Criteria.where("articleId").is(commentQuery.getArticleId()));
        }
        query.with(pageable);
        // 执行分页查询
        long total = mongoTemplate.count(query, Comment.class);
        java.util.List<Comment> comments = mongoTemplate.find(query, Comment.class);
        // 封装分页结果
        Page<Comment> commentPage = new PageImpl<>(comments, pageable, total);
        return R.ok(commentPage);
    }
}
